package com.vtc.session14;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vtc.R;
import com.vtc.session13.Student;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StudentAdapter extends BaseAdapter {

	private Context context;
	private List<Student> data;

	private String baseURL = "http://172.27.10.154/android/";
	public StudentAdapter(Context context) {
		this.context = context;
	}

	public void fetchAll() {
		String strData = NetworkRequest.doRequest(null,baseURL+"liststudent.php");
		try {
			data = new ArrayList<Student>();
			JSONArray dataJson = new JSONArray(strData);
			for (int i = 0; i < dataJson.length(); i++) {
				JSONObject obj = dataJson.getJSONObject(i);
				String name = obj.getString("fullname");
				String addr = obj.getString("address");
				int age = obj.getInt("age");
				int id = obj.getInt("id");
				data.add(new Student(id,name, addr, age));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void add(Student st) {
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair("fullname", st.getName()));
		data.add(new BasicNameValuePair("addr", st.getAddr()));
		data.add(new BasicNameValuePair("age", st.getAge() + ""));
		NetworkRequest.doRequest(data,baseURL+"studentadd.php");
		this.fetchAll();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);

	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		RelativeLayout layout = null;

		final Student st = data.get(position);
		if (view == null) {
			LayoutInflater inflat = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			layout = (RelativeLayout) inflat.inflate(
					R.layout.studentlist_rowitem, null);

		} else {
			layout = (RelativeLayout) view;
		}
		TextView txtName = (TextView) layout
				.findViewById(R.id.studentlist_rowitem_fullname);
		TextView txtAge = (TextView) layout
				.findViewById(R.id.studentlist_rowitem_age);
		CheckBox chCheck = (CheckBox) layout
				.findViewById(R.id.studentlist_rowitem_checkbox);
		chCheck.setChecked(st.isChecked());
		chCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				st.setChecked(isChecked);
			}
		});
		txtName.setText(st.getName());
		txtAge.setText(st.getAge()+"");

		return layout;
	}
	public void delete(){
		String ids = "";
		for(Student st:data){
			if(st.isChecked()){
				ids += st.getId()+",";
			}
		}
		System.out.println(ids);
		ids = ids.substring(0,ids.length()-1);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("ids", ids));
		NetworkRequest.doRequest(params,baseURL+"studentdelete.php");
		this.fetchAll();
		notifyDataSetChanged();
		
	}

	public void update(Student vo) {
		List<NameValuePair> data = new ArrayList<NameValuePair>();
		data.add(new BasicNameValuePair("fullname", vo.getName()));
		data.add(new BasicNameValuePair("addr", vo.getAddr()));
		data.add(new BasicNameValuePair("age", vo.getAge() + ""));
		data.add(new BasicNameValuePair("id", vo.getId() + ""));
		NetworkRequest.doRequest(data,baseURL+"studentupdate.php");
		this.fetchAll();
		notifyDataSetChanged();
	}

}
